package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.OptimizedHouseDAO;
import com.foreveross.vo.OptimizedHouse;

@Component("optimizedHouseDAO")
public class OptimizedHouseDAOImp extends SQLUtil implements OptimizedHouseDAO {

	@Override
	public long findOptimizedHouseListCount(String optimizedId, String houseId)
			throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select count(distinct a.id) from optimized_house a ");
		sqlStr.append(" where 1=1 ");
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and a.house_id ='" + houseId + "'");
		}
		if(null!=optimizedId && !"".equals(optimizedId)){
			sqlStr.append(" and a.optimized_id = '" + optimizedId + "'");
		}
		return Long.parseLong(findObejctBySQL(sqlStr.toString(),null).get(0).toString()) ;
	}
	
	@Override
	public List<OptimizedHouse> findOptimizedHouseList(String optimizedId,
			String houseId,int pageIndex,int pageSize) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select distinct a.* from optimized_house a ");
		sqlStr.append(" where 1=1 ");
		
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and a.house_id ='" + houseId + "'");
		}
		if(null!=optimizedId && !"".equals(optimizedId)){
			sqlStr.append(" and a.optimized_id = '" + optimizedId + "'");
		}
		return findObejctBySQL(sqlStr.toString(),OptimizedHouse.class, pageIndex,
				pageSize);
	}

	@Override
	public void deleteOptimizedHouse(String id) throws Exception {
		final String hql = "delete from OptimizedHouse t where t.id='" + id + "'";
		updateObejctByHql(hql);
	}

	@Override
	public void deleteOptimizedHouseByOptimizedId(String optimizedId)
			throws Exception {
		final String hql = "delete from OptimizedHouse t where t.optimized.id='" + optimizedId + "'";
		updateObejctByHql(hql);
	}

}
